package com.example.mall.web.controller;

import com.example.mall.domain.ProductSpecs;
import com.example.mall.dto.ProductSpecsDto;
import com.example.mall.exception.ErrorEnumCode;
import com.example.mall.exception.GlobalCustomerException;
import com.example.mall.query.PageDTO;
import com.example.mall.query.ProductSpecsQuery;
import com.example.mall.result.JSONResult;
import com.example.mall.service.IProductSpecsService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/productSpecs")
public class ProductSpecsController {

    @Autowired
    public IProductSpecsService productSpecsService;

    /**
     * 分页查询商品规格表信息
     * @param productSpecsQuery
     * @return
     */
    @GetMapping("queryPage")
    @ApiOperation(value = "分页查询商品规格表信息",notes = "分页查询商品规格表信息",httpMethod = "GET")
    public JSONResult queryPage(ProductSpecsQuery productSpecsQuery){
        PageDTO<ProductSpecsDto> loginPageDTO = productSpecsService.queryPage(productSpecsQuery);
        return JSONResult.success("查询成功",loginPageDTO);
    }

    /**
     * 根据id查询商品规格表信息
     * @param id
     * @return
     */
    @GetMapping("query/{id}")
    @ApiOperation(value = "根据id查询商品规格表信息",notes = "根据id查询商品规格表信息",httpMethod = "GET")
    public JSONResult query(@PathVariable("id")Long id){
        //1.根据id查询用户信息
        if (id == null){
            throw new GlobalCustomerException(ErrorEnumCode.SPECID_NOT_ID);
        }
        ProductSpecs productSpecs = productSpecsService.getById(id);
        return JSONResult.success("查询成功",productSpecs);
    }

    /**
     * 根据id查询商品规格表信息
     * @param id
     * @return
     */
    @GetMapping("queryProductSpecsById/{id}")
    @ApiOperation(value = "根据id查询商品规格表信息",notes = "根据id查询商品规格表信息",httpMethod = "GET")
    public JSONResult queryProductSpecsById(@PathVariable("id")Long id){
        //1.根据id查询用户信息
        if (id == null){
            throw new GlobalCustomerException(ErrorEnumCode.SPECID_NOT_ID);
        }
        ProductSpecs productSpecs = productSpecsService.queryProductSpecsById(id);
        return JSONResult.success("查询成功",productSpecs);
    }

    /**
     * 查询全部商品规格表信息
     * @param
     * @return
     */
    @GetMapping("queryProductSpecsList")
    @ApiOperation(value = "查询全部商品规格表信息",notes = "查询全部商品规格表信息",httpMethod = "GET")
    public JSONResult queryProductSpecsList(){
        List<ProductSpecs> productSpecs = productSpecsService.queryProductSpecsList();
        return JSONResult.success("查询成功",productSpecs);
    }

    /**
     * 新增商品规格表
     * @param productSpecsDto
     * @return
     */
    @PostMapping("insert")
    @ApiOperation(value = "新增商品规格表",notes = "新增商品规格表",httpMethod = "POST")
    public JSONResult insert(@RequestBody ProductSpecsDto productSpecsDto){
        productSpecsService.insert(productSpecsDto);
        return JSONResult.success("保存成功");
    }

    /**
     * 修改商品规格表信息
     * @param productSpecsDto
     * @return
     */
    @PostMapping("update")
    @ApiOperation(value = "修改商品规格表信息",notes = "修改商品规格表信息",httpMethod = "POST")
    public JSONResult update(@RequestBody ProductSpecsDto productSpecsDto){
        productSpecsService.update(productSpecsDto);
        return JSONResult.success("修改成功");
    }

    /**
     * 删除商品规格表信息
     * @param id
     * @return
     */
    @PostMapping("delete/{id}")
    @ApiOperation(value = "删除商品规格表信息",notes = "删除商品规格表信息",httpMethod = "POST")
    public JSONResult delete(@PathVariable("id") Long id) {
        productSpecsService.delete(id);
        return JSONResult.success("删除成功");
    }

    /**
     * 批量删除商品规格表信息
     * @param ids
     * @return
     */
    @PostMapping("deleteBatch")
    @ApiOperation(value = "批量删除商品规格表信息",notes = "批量删除商品规格表信息",httpMethod = "POST")
    public JSONResult deleteAll(@RequestBody List<Long> ids) {
        productSpecsService.deleteBatch(ids);
        return JSONResult.success("批量删除成功");
    }
}
